package basicservicelogic

import (
	"context"

	"property_service/dao/model"
	"property_service/internal/svc"
	"property_service/property_service"

	"property_service/dao/query"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type WarehouseAuthorizationAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWarehouseAuthorizationAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WarehouseAuthorizationAddLogic {
	return &WarehouseAuthorizationAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// WarehouseAuthorizationAdd 创建仓库授权表项
func (l *WarehouseAuthorizationAddLogic) WarehouseAuthorizationAdd(in *property_service.WarehouseAuthorization) (*property_service.Response, error) {
	// 获取仓库授权表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).WarehouseAuthorization
	// 创建一个新的仓库授权表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.WarehouseAuthorization{
		WarehouseID:      in.WarehouseID,      // 仓库id
		WarehouseName:    in.WarehouseName,    // 仓库名
		UserName:         in.UserName,         // 用户名
		NickName:         in.NickName,         // 昵称
		UserID:           in.UserID,           // 用户id
		Status:           in.Status,           // 状态
		DefaultWarehouse: in.DefaultWarehouse, // 是否默认
		Description:      in.Description,      // 说明
		TenantID:         in.TenantID,         // 租户ID
		CreatedAtuser:    in.CreatedAtuser,    // 创建人
		UpdatedAtuser:    in.UpdatedAtuser,    // 更新人
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
